/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIStarter.java
 *
 * Created on 17.07.2011, 15:23:19
 */

package kurs;

import java.awt.Container;
import java.awt.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Matrix
 */
public class GUIStarter extends javax.swing.JFrame {

    /** Creates new form GUIStarter */
    public GUIStarter() {
        root = new DynStackOfQueue();
        initComponents();
    }

    private void refresh(java.awt.event.ActionEvent evt) {
        try {
            structureTextArea.setText(root.printFullState());
        } catch (EmptyException eE) {
            //JOptionPane.showMessageDialog(rootPane, eE.getMessage(), "Info", WIDTH);
            structureTextArea.setText(null);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jOptionPane1 = new javax.swing.JOptionPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        structureTextArea = new javax.swing.JTextArea();
        elementTextField = new javax.swing.JTextField();
        findButton = new javax.swing.JButton();
        pushButton = new javax.swing.JButton();
        popButton = new javax.swing.JButton();
        enqueueButton = new javax.swing.JButton();
        dequeueButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Dynamic Structure Control Panel");
        setAlwaysOnTop(true);
        setResizable(false);

        structureTextArea.setColumns(20);
        structureTextArea.setEditable(false);
        structureTextArea.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        structureTextArea.setRows(5);
        jScrollPane1.setViewportView(structureTextArea);

        elementTextField.setText("Enter element");

        findButton.setText("Find");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });

        pushButton.setText("Push");
        pushButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pushButtonActionPerformed(evt);
            }
        });

        popButton.setText("Pop");
        popButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popButtonActionPerformed(evt);
            }
        });

        enqueueButton.setText("Enqueue");
        enqueueButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enqueueButtonActionPerformed(evt);
            }
        });

        dequeueButton.setText("Dequeue");
        dequeueButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dequeueButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(elementTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pushButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(findButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(popButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(enqueueButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dequeueButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {elementTextField, enqueueButton, pushButton});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {dequeueButton, findButton, popButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(elementTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(findButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(pushButton)
                            .addComponent(popButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(enqueueButton)
                            .addComponent(dequeueButton))
                        .addGap(243, 243, 243)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pushButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pushButtonActionPerformed
        // TODO add your handling code here:
        String addedElementInfo = elementTextField.getText();
        if (addedElementInfo.equalsIgnoreCase("")||(addedElementInfo.equalsIgnoreCase("Enter element"))) {
            root.push(new HardNode("null"));
        } else {
            root.push(new HardNode(addedElementInfo));
        }
        refresh(evt);
    }//GEN-LAST:event_pushButtonActionPerformed

    private void enqueueButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enqueueButtonActionPerformed
        // TODO add your handling code here:
        try {
            String addedElementInfo = elementTextField.getText();
            if (addedElementInfo.equalsIgnoreCase("")||(addedElementInfo.equalsIgnoreCase("Enter element"))) {
                ((HardNode)root.top()).queue.enqueue(new BaseNode("null"));
            } else {
                ((HardNode)root.top()).queue.enqueue(new BaseNode(addedElementInfo));
            }
            refresh(evt);
        } catch (EmptyException eE) {
            JOptionPane.showMessageDialog(rootPane, eE.getMessage(), "Info", WIDTH);
        }
    }//GEN-LAST:event_enqueueButtonActionPerformed

    private void popButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popButtonActionPerformed
        // TODO add your handling code here:
        try {
            HardNode temp = (HardNode)root.pop();
            refresh(evt);
            if (temp.queue.isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "Poped element: " + temp.getInfo() +"\n Queue was empty", "Info", WIDTH);
            } else {
                JOptionPane.showMessageDialog(rootPane, "Poped element: " + temp.getInfo() +"\n" + temp.queue.printState(), "Info", WIDTH);
            }
        } catch (EmptyException eE) {
            JOptionPane.showMessageDialog(rootPane, eE.getMessage(), "Info", WIDTH);
        }
    }//GEN-LAST:event_popButtonActionPerformed

    private void dequeueButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dequeueButtonActionPerformed
        // TODO add your handling code here:
        try {
            BaseNode temp = ((HardNode)root.top()).queue.dequeue();
            refresh(evt);
            JOptionPane.showMessageDialog(rootPane, "Dequeued element: " + temp.getInfo(), "Info", WIDTH);
        } catch (EmptyException eE) {
            JOptionPane.showMessageDialog(rootPane, eE.getMessage(), "Info", WIDTH);
        }
    }//GEN-LAST:event_dequeueButtonActionPerformed

    private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findButtonActionPerformed
        // TODO add your handling code here:
        try {
            JOptionPane.showMessageDialog(rootPane,"Element finded: " + root.fullFind(elementTextField.getText()).getInfo(), "Info", WIDTH);
        } catch (NotFoundException nFE) {
            JOptionPane.showMessageDialog(rootPane, nFE.getMessage() + ": " + elementTextField.getText(), "Info", WIDTH);
        } catch (EmptyException eE) {
            JOptionPane.showMessageDialog(rootPane, eE.getMessage(), "Info", WIDTH);
        }

    }//GEN-LAST:event_findButtonActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new GUIStarter().setVisible(true);
//            }
//        });
//
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton dequeueButton;
    private javax.swing.JTextField elementTextField;
    private javax.swing.JButton enqueueButton;
    private javax.swing.JButton findButton;
    private javax.swing.JOptionPane jOptionPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton popButton;
    private javax.swing.JButton pushButton;
    private javax.swing.JTextArea structureTextArea;
    // End of variables declaration//GEN-END:variables
    DynStackOfQueue root;
}
